package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueChargeOffsMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueChargeOffsService;
import com.zjdiepu.www.model.dh.dto.DhOverdueChargeOffs;
import com.zjdiepu.www.model.dh.ext.DhOverdueChargeOffsExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月02日 18时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueChargeOffsServiceImpl extends BaseService implements IDhOverdueChargeOffsService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueChargeOffsServiceImpl.class);

	@Autowired
	private DhOverdueChargeOffsMapper dhOverdueChargeOffsMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueChargeOffs dhOverdueChargeOffs, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueChargeOffs.initInsertData(dhOverdueChargeOffs.getModifier(), dhOverdueChargeOffs.getModifierName());
			dhOverdueChargeOffsMapper.insert(dhOverdueChargeOffs);
			break;
		case edit:
			dhOverdueChargeOffs.initUpdateData(dhOverdueChargeOffs.getModifier(), dhOverdueChargeOffs.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueChargeOffsMapper.updateByPrimaryKeySelective(dhOverdueChargeOffs);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueChargeOffs.initDeleteData(dhOverdueChargeOffs.getModifier(), dhOverdueChargeOffs.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueChargeOffsMapper.updateByPrimaryKeySelective(dhOverdueChargeOffs);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueChargeOffs> selectByExt(DhOverdueChargeOffsExt dhOverdueChargeOffsExt) {
		return dhOverdueChargeOffsMapper.selectByExt(dhOverdueChargeOffsExt);
	}
	
	@Override
	public int countByExt(DhOverdueChargeOffsExt dhOverdueChargeOffsExt){
		return dhOverdueChargeOffsMapper.countByExt(dhOverdueChargeOffsExt);
	}
	
	@Override
	public DhOverdueChargeOffs selectById(String id) {
		return dhOverdueChargeOffsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueChargeOffsMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueChargeOffs dhOverdueChargeOffs) {
		return dhOverdueChargeOffsMapper.countRefined(dhOverdueChargeOffs);
	}

	@Override
	public DhOverdueChargeOffs selectRefined(DhOverdueChargeOffs dhOverdueChargeOffs) {
		return dhOverdueChargeOffsMapper.selectRefined(dhOverdueChargeOffs);
	}

	@Override
	public boolean repeat(DhOverdueChargeOffs dhOverdueChargeOffs) {
		int count = countRefined(dhOverdueChargeOffs);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueChargeOffs.getId())) {
			return true;
		}
		DhOverdueChargeOffs dbItem = selectRefined(dhOverdueChargeOffs);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueChargeOffs.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueChargeOffsMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
